package com.sh.service.user.impl;

import com.sh.common.context.AppContextHolder;
import com.sh.common.exception.BizException;
import com.sh.entity.Bucket;
import com.sh.entity.ObjectItem;
import com.sh.pojo.bo.CopyObjectsBO;
import com.sh.pojo.bo.DeleteObjectBO;
import com.sh.pojo.bo.SearchObjectBO;
import com.sh.pojo.vo.DownLoadObjectVO;
import com.sh.service.permission.IUserPermissionService;
import com.sh.service.store.IOSService;
import com.sh.service.user.IUserResourceService;
import com.sh.utils.PathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

@Service
@Slf4j
public class UserResourceService implements IUserResourceService {
    @Autowired
    private IOSService osService;
    @Autowired
    private IUserPermissionService userPermissionService;

    @Override
    public List<String> listUserBuckets() {
        String userId = AppContextHolder.getUserId();
        Boolean isAdmin = AppContextHolder.getAdmin();
        if (isAdmin) {
            List<String> bucketList = new ArrayList<>();
            List<Bucket> buckets = osService.listBuckets();
            if (buckets == null || buckets.isEmpty()) {
                return bucketList;
            }
            buckets.forEach(e -> {
                bucketList.add(e.getName());
            });
            return bucketList;

        }
        return userPermissionService.listBucketByUser(userId);
    }

    @Override
    public List<String> listUserFirstFolders(String bucket) {
        //查询数据库最高级的level
        return userPermissionService.getUserMaxPermission(bucket);
    }

    @Override
    public List<ObjectItem> searchObjects(SearchObjectBO bo) throws BizException {
        return osService.listObjects(bo.getBucket(), bo.getPath(), bo.getPattern());
    }

    @Override
    public String getDownloadUrl(String bucket, String objNm) {
        return null;
    }

    @Override
    public void uploadObject(MultipartFile file, String bucket, String path, String objectName) throws BizException {
        osService.putObject(bucket, PathUtil.assembleObjectKey(path, objectName), file);

    }

    @Override
    public void createFolder(String bucket, String path, String folderName) throws BizException {
        osService.createFolder(bucket, PathUtil.getPathEndWithSeparator(PathUtil.assembleObjectKey(path, folderName)));
    }

    @Override
    public void deleteObject(String bucket, String path, String objectName) throws BizException {
        osService.deleteObject(bucket, PathUtil.assembleObjectKey(path, objectName));
    }

    @Override
    public void deleteFolder(String bucket, String path) throws BizException {
        osService.deleteFolder(bucket, PathUtil.getPathNotEndWithSeparator(path));
    }

    @Override
    public void deleteObjects(String bucket, List<String> objectKeys) throws BizException {
        if (objectKeys == null || objectKeys.isEmpty()) {
            return;
        }
        List<String> objects = new ArrayList<>();
        objectKeys.forEach(o -> {
            objects.add(PathUtil.assembleObjectKey(o));
        });

        osService.deleteObjects(bucket, objects);
    }

    @Override
    public void renameFolder(String bucket, String path, String sourceName, String targetName) throws BizException {
        osService.renameFolder(bucket, PathUtil.assembleObjectKey(path, sourceName), PathUtil.assembleObjectKey(path, targetName));
    }

    @Override
    public void renameObject(String bucket, String path, String sourceName, String targetName) throws BizException {
        osService.renameObject(bucket, PathUtil.assembleObjectKey(path, sourceName), PathUtil.assembleObjectKey(path, targetName));
    }

    @Override
    public void copyObject(String bucket, String path, String objectName, String targetObjectName) throws BizException {
        osService.copyObject(bucket, PathUtil.assembleObjectKey(path, objectName), PathUtil.assembleObjectKey(path, targetObjectName));
    }

    @Override
    public void copyObjects( CopyObjectsBO copyObjectsBO) {
        osService.copyObjects(copyObjectsBO);
    }

    @Override
    public void uploadFolder(String bucket, String path, List<MultipartFile> files) throws BizException {
        if (files == null || files.isEmpty()) {
            return;
        }
        for (MultipartFile f : files) {
            String objectKey = PathUtil.assembleObjectKey(path, f.getOriginalFilename());
            osService.putObject(bucket, objectKey, f);
        }
    }

    @Override
    public List<DownLoadObjectVO> downloadObjects(String bucket, List<String> objectKeys, Integer expire, String downloadPath) throws BizException {
       List<String> formatList=new ArrayList<>();
        if (objectKeys != null && !objectKeys.isEmpty()) {
            objectKeys.forEach(o->{
                formatList.add(PathUtil.assembleObjectKey(o));
            });
        }
       return osService.downloadObjects(bucket, objectKeys, expire, downloadPath);
    }

    @Override
    public ByteArrayOutputStream downloadAndZipFiles(String bucket, List<String> paths) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
            for (String path : paths) {
                osService.downloadAndZipObjects(bucket, path, zos);
            }
        }
        return baos;
    }
}
